package com.hzncc.flowable_diboot.flowable.servicetask.payment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.core.util.ContextHolder;
import com.hzncc.flowable_diboot.entity.SapCustomer;
import com.hzncc.flowable_diboot.payment.entity.OldReceive;
import com.hzncc.flowable_diboot.payment.entity.PaymentContract;
import com.hzncc.flowable_diboot.payment.entity.PaymentRegister;
import com.hzncc.flowable_diboot.payment.service.OldReceiveService;
import com.hzncc.flowable_diboot.payment.service.PaymentContractService;
import com.hzncc.flowable_diboot.payment.service.PaymentRegisterService;
import com.hzncc.flowable_diboot.payment.service.impl.OldReceiveServiceImpl;
import com.hzncc.flowable_diboot.payment.service.impl.PaymentContractServiceImpl;
import com.hzncc.flowable_diboot.payment.service.impl.PaymentRegisterServiceImpl;
import com.hzncc.flowable_diboot.service.SapCustomerService;
import com.hzncc.flowable_diboot.service.impl.SapCustomerServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 更改回款登记金额&锁定回款登记
 */
@Component
public class UpdatePaymentRegisterAmount implements JavaDelegate  {

    @Override
    @Transactional
    public void execute(DelegateExecution execution) {

        PaymentContractService paymentContractService = ContextHolder.getBean(PaymentContractServiceImpl.class);
        PaymentRegisterService paymentRegisterService = ContextHolder.getBean(PaymentRegisterServiceImpl.class);
        SapCustomerService sapCustomerService = ContextHolder.getBean(SapCustomerServiceImpl.class);
        OldReceiveService oldReceiveService = ContextHolder.getBean(OldReceiveServiceImpl.class);

        //当前正在执行任务对应的流程实例
        String instanceId = execution.getProcessInstanceId();
        LambdaQueryWrapper<PaymentContract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentContract::getInstanceId,instanceId).eq(PaymentContract::isDeleted,0);
        ThreadLocalHolder.setIgnoreInterceptor();
        PaymentContract paymentContract = paymentContractService.getSingleEntity(queryWrapper);
        ThreadLocalHolder.setIgnoreInterceptor();
        PaymentRegister paymentRegister = paymentRegisterService.getEntity(paymentContract.getPaymentRegisterId());

        //更新回款登记金额&锁定回款登记
        BigDecimal paymentAmount = paymentRegister.getPaymentAmount();
        BigDecimal receivedAmount = paymentRegister.getReceivedAmount();
        BigDecimal unclaimedAmount;
        BigDecimal currentReceivedAmount = paymentContract.getCurrentReceivedAmount();
        if (ObjectUtils.isNotEmpty(paymentContract.getCommission())) {
            currentReceivedAmount = currentReceivedAmount.subtract(paymentContract.getCommission());
        }
        if (ObjectUtils.isNotEmpty(paymentAmount)&&
                ObjectUtils.isNotEmpty(receivedAmount)&&
                ObjectUtils.isNotEmpty(currentReceivedAmount)){
            receivedAmount = receivedAmount.add(currentReceivedAmount);
            unclaimedAmount = paymentAmount.subtract(receivedAmount);
            String paymentStatus = "10";
            if (receivedAmount.compareTo(new BigDecimal(0))>0&&unclaimedAmount.compareTo(new BigDecimal(0))>0){
                paymentStatus = "20";
            }else if (unclaimedAmount.compareTo(new BigDecimal(0))<=0){
                paymentStatus = "30";
            }
            LambdaUpdateWrapper<PaymentRegister> paymentRegisterUpdate = new LambdaUpdateWrapper<>();
            paymentRegisterUpdate.set(PaymentRegister::getReceivedAmount,receivedAmount)
                    .set(PaymentRegister::getUnclaimedAmount,unclaimedAmount)
                    .set(PaymentRegister::getStatus,paymentStatus)
                    .set(PaymentRegister::getLocked,1).eq(PaymentRegister::getId,paymentContract.getPaymentRegisterId());
            ThreadLocalHolder.setIgnoreInterceptor();
            paymentRegisterService.updateEntity(paymentRegisterUpdate);
        }
        //如果是老应收，更新老应收回款金额
//        if ("30".equals(paymentContract.getPaymentContractCategory())){
//            ThreadLocalHolder.setIgnoreInterceptor();
//            SapCustomer sapCustomer = sapCustomerService.getEntity(paymentContract.getSapCustomer());
//            String sapNo = sapCustomer.getSapNo();
//            String companyCode = sapCustomer.getCompanyCode();
//            LambdaQueryWrapper<OldReceive> oldReceiveQuery = new LambdaQueryWrapper<>();
//            oldReceiveQuery.eq(OldReceive::getCompanyCode,companyCode).eq(OldReceive::getSapNo,sapNo).eq(OldReceive::isDeleted,0);
//            ThreadLocalHolder.setIgnoreInterceptor();
//            OldReceive oldReceive = oldReceiveService.getSingleEntity(oldReceiveQuery);
//            oldReceive.setReceivedAmount(oldReceive.getReceivedAmount().add(paymentContract.getCurrentReceivedAmount()));
//            oldReceive.setUnreceivedAmount(oldReceive.getReceivableAmount().subtract(oldReceive.getReceivedAmount()));
//            ThreadLocalHolder.setIgnoreInterceptor();
//            oldReceiveService.updateEntity(oldReceive);
//        }

    }

}
